define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var $ = require('jquery');
	var input = require('@base/common/input');

	// 出价权限
	var ChargePriority = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'P-infoChargePriority'
				,'items': [
					{
						'text': LANG('CPM:'),
						'value': 1,
						'options': [
							{'text': LANG('CPM竞价'), 'value': 0},
							{'text': LANG('固定CPM'), 'value': 4}
						]
					},
					{
						'text': LANG('CPC:'),
						'value': 2,
						'options': [
							{'text': LANG('CPC优化'), 'value': 2},
							{'text': LANG('固定CPC'), 'value': 3}
						]
					},
					{
						'text': LANG('CPA:'),
						'value': 3,
						'options': [
							{'text': LANG('CPA优化'), 'value': 1}
						]
					}
				]
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			var self = this;
			var c = self.getConfig();
			var doms = self.$doms = {};
			var el = self.getDOM();

			doms.content = $('<div class="P-infoChargePriorityCon"/>').appendTo(el);

			if(c.items && c.items.length){
				util.each(c.items, function(item, idx){
					if(item){
						var con = self.create(item.value+'checkbox', input.checkbox,{
							'label': null,
							'data': [{text:item.text,value:item.value}],
							'target': doms.content,
							'changeEvent': true,
							'autoChange': false,
							'eventWithoutChange': true,
							'labelWidth': 80
						}).reset();
						self.create(item.value+'radio', input.radio,{
							'class': 'M-formItem radioGroup',
							'label': null,
							'data': item.options,
							'target': con.getDOM(),
							'labelWidth': 90
						}).disable(true);
					}
				});
			}

			// this.setData([
			// 	{type:1,childType:0},
			// 	{type:2,childType:2},
			// 	{type:3,childType:1}
			// ]);
		},
		onRadioChange: function(evt){
			var check = $(evt.param.el).prop('checked');
			var name = evt.param.value+'radio';
			this.$[name].disable(!check);
			if(!check){
				this.$[name].reset();
			}
		},
		getData: function(){
			var self = this;
			var data = [];
			var c = self.getConfig();
			util.each(c.items, function(item, idx){
				if(item){
					var type = self.$[item.value+'checkbox'].getData()[0];
					var childType = self.$[item.value+'radio'].getData();
					if(type && (childType === '0' || childType)){
						data.push({
							type: +type,
							childType: +childType
						});
					}
				}
			});

			return data;
		},
		setData: function(data){
			if(data){
				var self = this;
				var c = self.getConfig();
				util.each(data, function(childData){
					util.each(c.items, function(item, idx){
						if(item && item.value == childData.type){
							self.$[item.value+'checkbox'].setData([childData.type]);
							self.$[item.value+'radio'].disable(false).setData(childData.childType);
						}
					});
				});
			}
			return this;
		},
		reset: function(){
			var self = this;
			var c = self.getConfig();
			util.each(c.items, function(item, idx){
				if(item){
					self.$[item.value+'checkbox'].reset();
					self.$[item.value+'radio'].reset();
				}
			});
			self.setData([
				{type:1,childType:0},
				{type:2,childType:2},
				{type:3,childType:1}
			]);
			return self;
		}
	});
	exports.base = ChargePriority;
});